# File:     regression_toxicity.R
# Purpose:  This script estimates differences in the toxicity experienced by Yellen versus other speakers
# Input:    /Data/finalData.RData
# Output:   /Paper/Figures/SI_toxicity_full.pdf
#           /Paper/Figures/interruptions_attack.pdf
#           /Paper/Figures/toxicity_interrupt_mfx_daughters.pdf
#           /Paper/Figures/toxicity_interrupt_mfx_daughters_SI.pdf
# Author:   JB


rm(list = ls())
require(lme4)
require(lfe)
require(tidyverse)
require(ggridges)

# source('./helper_functions.R')

load('D:/Dropbox/FED/FED/Paper/JOP/RR1_replication/Data/finalData.RData')


# JOP RR1: are yellen's interruptions more toxic?

test <- utterance_level %>%
  select(respondingTo,interruptor,matches('SENT_')) %>%
  select(-matches('SENT_.*lag'),-SENT_error) %>%
  # filter(grepl('FED',respondingTo)) %>%
  gather(sentiment,value,-respondingTo,-interruptor)

test %>%
  group_by(respondingTo,sentiment,interruptor) %>%
  mutate(id = row_number(),
         nUtt = n()) %>%
  filter(nUtt > 10) %>%
  select(-nUtt) %>%
  filter(!grepl('unsub|incoh|rejec|comb|author',tolower(sentiment))) %>%
  ggplot(aes(x = value,y = sentiment,color = factor(interruptor))) +
  geom_boxplot(outlier.colour = NA)


pdf('../output/figures/SI_negative_interruptions.pdf',width = 7,height = 5)
test %>%
  group_by(respondingTo,sentiment,interruptor) %>%
  mutate(id = row_number(),
         nUtt = n()) %>%
  filter(nUtt > 2) %>%
  select(-nUtt) %>%
  # filter(!grepl('unsub|incoh|rejec|comb|author',tolower(sentiment))) %>%
  spread(interruptor,value,sep = '_') %>%
  drop_na() %>%
  mutate(diff = interruptor_1 - interruptor_0) %>%
  group_by(sentiment) %>%
  summarise(diff = mean(diff,na.rm=T),
            pval = t.test(interruptor_1,interruptor_0)$p.value,
            tstat = t.test(interruptor_1,interruptor_0)$statistic) %>%
  ungroup() %>%
  mutate(se = diff/tstat) %>%
  # filter(sentiment == 'SENT_combAttack') %>%
  filter(!grepl('autho|incoh|unsubs|reject',tolower(sentiment))) %>%
  ggplot(aes(x = diff,y = reorder(sentiment,diff))) + 
  geom_point() + 
  geom_errorbarh(aes(xmin = diff - 2*se,xmax = diff + 2*se),height = 0) + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  labs(x = 'Difference',
       y = 'Tone',
       title = 'Differences between interruptions and other utterances',
       subtitle = 'T-test difference of means by tone') + 
  theme_bw()
dev.off()

require(fixest)

m <- feols(SENT_combAttack ~ respondingTo*factor(interruptor) + log(nchars) + log(nchars_lag) + factor(interrupted) | opensecretsID,
      utterance_level %>%
        filter(all > 30,ind > mind) %>%
        # filter(grepl('FED',respondingTo)) %>%
        # mutate(respondingTo = ifelse(!grepl('FED',respondingTo),'Others',respondingTo)) %>%
        # mutate(respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE')) %>%
        filter(!grepl('FED',opensecretsID)) %>%
        filter(grepl('FED',respondingTo)) %>%
        mutate(respondingTo = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                               ifelse(grepl('FED',respondingTo),'Male Fed Chairs',NA))) %>%
        mutate(respondingTo = relevel(factor(respondingTo),ref = 'Male Fed Chairs'))
        
      ,
      cluster = 'respondingTo')
m
require(marginaleffects)
toplotCAP <- plot_cap(m,condition = list('respondingTo','interruptor'),draw = F)
toplotCME <- plot_cme(m,variables = 'respondingTo',condition = list('interruptor'),draw = F)

pCAP <- toplotCAP %>%
  as_tibble() %>%
  ggplot(aes(x = respondingTo,y = estimate,color = factor(interruptor),
             shape = factor(interruptor))) + 
  geom_point(position = position_dodge(width = .1),size = 2.5) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = .05,
                position = position_dodge(width = .1)) + 
  scale_color_manual(name = 'Utterance',values = c('grey40','black'),labels = c('Non-Interruption','Interruption')) + 
  scale_shape_discrete(name = 'Utterance',labels = c('Non-Interruption','Interruption')) + 
  labs(x = 'Responding To...',
       y = 'Predicted Aggression',
       title = 'Predicted Values',
       subtitle = 'Aggression by utterance') + 
  theme_bw()


(pCME <- toplotCME %>%
  ggplot(aes(x = factor(interruptor),y = estimate,color = factor(interruptor),shape = factor(interruptor))) + 
  geom_point(size = 2.5) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = .05) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  theme_bw() + 
    scale_color_manual(name = 'Utterance',values = c('grey40','black'),labels = c('Non-Interruption','Interruption')) + 
    scale_shape_discrete(name = 'Utterance',labels = c('Non-Interruption','Interruption')) + 
  scale_x_discrete(name = 'Utterance',labels = c('Non-interruption','Interruption')) + 
  labs(y = 'Coefficient Estimate',
       title = 'Marginal Effects',
       subtitle = 'Yellen vs Fed Chairs'))

require(patchwork)

pdf('../output/figures/SI_interruptions_fed.pdf',width = 7,height = 4)
pCAP + pCME + plot_layout(guides = 'collect') & theme(legend.position = 'bottom')
dev.off()

# Quantile regression
# First, fine the quantile that corresponds to the standard OLS
require(quantreg)
which.min(abs(mean(utterance_level %>%
       filter(all > 30,ind > mind) %>%
       filter(!grepl('FED',opensecretsID)) %>%
       filter(grepl('FED',respondingTo)) %>%
       mutate(respondingTo = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                                    ifelse(grepl('FED',respondingTo),'Male Fed Chairs',NA))) %>%
       mutate(respondingTo = relevel(factor(respondingTo),ref = 'Male Fed Chairs')) %>%
       pull(SENT_combAttack)) - quantile(utterance_level %>%
           filter(all > 30,ind > mind) %>%
           filter(!grepl('FED',opensecretsID)) %>%
           filter(grepl('FED',respondingTo)) %>%
           mutate(respondingTo = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                                        ifelse(grepl('FED',respondingTo),'Male Fed Chairs',NA))) %>%
           mutate(respondingTo = relevel(factor(respondingTo),ref = 'Male Fed Chairs')) %>%
           pull(SENT_combAttack),p = seq(.668897,.668899,by = .00000001))))


utterance_level %>%
  filter(all > 30,ind > mind) %>%
  filter(!grepl('FED',opensecretsID)) %>%
  filter(grepl('FED',respondingTo)) %>%
  mutate(respondingTo = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                               ifelse(grepl('FED',respondingTo),'Male Fed Chairs',NA))) %>%
  mutate(respondingTo = relevel(factor(respondingTo),ref = 'Male Fed Chairs')) %>%
  ggplot(aes(x = SENT_combAttack)) + 
  geom_histogram() + 
  geom_vline(aes(xintercept = quantile(SENT_combAttack,.25,na.rm=T)))
# Roughly 68.04
toplotRQ <- NULL
for(tau in c(.6688979,seq(.3,.9,by = .1))) {
  # stop()
  mRQ <- rq(SENT_combAttack ~ respondingTo*factor(interruptor) + log(nchars) + log(nchars_lag) + factor(interrupted) + 
              opensecretsID,
            tau = tau,
            data = utterance_level %>%
              filter(all > 30,ind > mind) %>%
              filter(!grepl('FED',opensecretsID)) %>%
              filter(grepl('FED',respondingTo)) %>%
              mutate(respondingTo = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                                           ifelse(grepl('FED',respondingTo),'Male Fed Chairs',NA))) %>%
              mutate(respondingTo = relevel(factor(respondingTo),ref = 'Male Fed Chairs'))
  )
  summary(mRQ)
  tmp <- plot_cme(mRQ,variables = 'respondingTo',condition = 'interruptor',draw = F)
  toplotRQ <- toplotRQ %>%
    bind_rows(tmp %>%
    mutate(tau = tau))
}


pdf('../output/figures/SI_aggression_quantile.pdf',width = 7,height = 5)
toplotRQ %>%
  as_tibble() %>%
  filter(tau != .6688979) %>%
  ggplot(aes(x = tau,y = estimate,color = factor(interruptor),shape = factor(interruptor))) + 
  geom_point(position = position_dodge(width = .005),size = 2.5) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),
                position = position_dodge(width = .005),width = .005) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  scale_color_manual(name = 'Utterance',values = c('grey50','black'),labels = c('Non-Interruption','Interruption')) + 
  scale_shape_manual(name = 'Utterance',values = c(19,17),labels = c('Non-Interruption','Interruption')) + 
  theme_bw() + 
  theme(legend.position = 'bottom') + 
  # geom_rect(xmin = .659,xmax = .68,ymin = -.05,ymax = .05,fill = NA,color = 'red',linetype = 'dashed') + 
  labs(x = 'Quantile',
       y = 'Marginal Effect of Yellen\nRelative to Male Fed Chairs',
       title = 'Quantile Regression Results',
       subtitle = 'Marginal effects by decile of aggression outcome')
dev.off()

# Is Yellen more toxic?
dyadToAnal <- utterance_level %>%
  arrange(fullInd) %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         yellen = ifelse(grepl('YELLEN',opensecretsID),1,0),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'),
         opensecretsID = relevel(factor(opensecretsID),ref = 'FEDYELLEN'),
         respGroup = ifelse(grepl('YELLEN',respondingTo),'Yellen',
                                  ifelse(grepl('BERNANKE',respondingTo),'Bernanke',
                                               ifelse(grepl('GREENSPAN',respondingTo),'Greenspan',
                                                      ifelse(grepl('POWELL',respondingTo),'Powell',lag(party)))))) %>% 
  filter(ind > mind,all > 30)


dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))
# Is yellen more toxic, attacking, or incoherent compared to other fed chairs?
toplot <- NULL
mList <- list()
for(y in dims) {
  (mList[[y]] <- feols(as.formula(paste0('scale(',y,') ~ yellen + ',
                                        # paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                        # ' + ',
                                        # paste(paste0('scale(',dims[-which(grepl('comb',dims)|dims == y)],'_lag)'),collapse = ' + '),
                                        ' + scale(log(nchars_lag+1)) + scale(log(tot_utterances)) + interrupted + interruptor',
                                        '| respondingTo + chamber')),
                      dyadToAnal %>%
                        filter(grepl('FED',opensecretsID),
                               !grepl('FED',respondingTo)),cluster = c('opensecretsID')))
  
  toplot <- bind_rows(toplot,mList[[y]]$coeftable %>%
                        data.frame() %>%
                        rename(est = Estimate,se = Std..Error,tstat = t.value,pval = Pr...t..) %>%
                        slice(1) %>%
                        mutate(out = y,
                               model = 'ols') %>% as_tibble())
}


# And the reverse!
dyadToAnal <- utterance_level %>%
  arrange(fullInd) %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         yellen = ifelse(grepl('YELLEN',respondingTo),1,0),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDYELLEN'),
         opensecretsID = relevel(factor(opensecretsID),ref = 'FEDYELLEN'),
         respGroup = relevel(factor(ifelse(grepl('YELLEN',respondingTo),'Yellen',
                            ifelse(grepl('BERNANKE',respondingTo),'Bernanke',
                                   ifelse(grepl('GREENSPAN',respondingTo),'Greenspan',
                                          ifelse(grepl('POWELL',respondingTo),'Powell',lag(party)))))),ref = 'Yellen')) %>% 
  filter(ind > mind,all > 30)


# Is Yellen having a worse time than other Fed chairs overall?
toplot <- toplot %>%
  mutate(model = 'fedTone')
mListRev <- list()
for(y in dims) {
  (mListRev[[y]] <- feols(as.formula(paste0('scale(',y,') ~ yellen + ',
                                            # paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            # ' + ',
                                            # paste(paste0('scale(',dims[-which(grepl('comb',dims)|dims == y)],'_lag)'),collapse = ' + '),
                                            ' + scale(log(nchars_lag+1)) + scale(log(tot_utterances)) + interrupted + interruptor',
                                        '| opensecretsID + chamber')),
                      dyadToAnal %>%
                        filter(!grepl('FED',opensecretsID),
                               grepl('FED',respondingTo)),cluster = c('respondingTo')))
  
  toplot <- bind_rows(toplot,mListRev[[y]]$coeftable %>%
                        data.frame() %>%
                        rename(est = Estimate,se = Std..Error,tstat = t.value,pval = Pr...t..) %>%
                        slice(1) %>%
                        mutate(out = y,
                               model = 'legTone') %>% as_tibble())
}


toplot %>%
  mutate(sentType = ifelse(out %in% paste0('SENT_',c('ATTACK_ON_AUTHOR','ATTACK_ON_COMMENTER','IDENTITY_ATTACK','INSULT','THREAT','combAttack')),'Aggression',
                           ifelse(out %in% paste0('SENT_',c('TOXICITY','SEVERE_TOXICITY','PROFANITY','SEXUALLY_EXPLICIT','FLIRTATION','OBSCENE','INFLAMMATORY','combToxic')),'Toxicity',
                                  ifelse(out %in% paste0('SENT_',c('INCOHERENT','UNSUBSTANTIAL','combIncoh')),'Incoherence',NA))),
         out = gsub('Attack On Commenter','Attack',
                    gsub('Combattack','INDEX: Aggression',
                         gsub('Combincoh','INDEX: Incoherence',
                              gsub('Combtoxic','INDEX: Toxicity',str_to_title(gsub('_',' ',gsub('SENT_','',out)))))))) %>%
  mutate(index = ifelse(grepl('INDEX',out),'index','dimension')) %>%
  mutate(model = ifelse(model == 'fedTone',"Tone of Fed Chair's Utterances","Tone of Utterances Responding to Fed Chair")) %>%
  filter(!is.na(sentType),
         !out %in% c('Attack On Author','Severe Toxicity'),
         !grepl('INDEX:',out)) %>%
  ggplot(aes(x = est,y = reorder(out,est),shape = index,fill = index,size = index)) + 
  geom_errorbarh(aes(xmin = est - 2*se,xmax = est + 2*se),height = .1,size = .5) + 
  geom_point() + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  theme_ridges() + 
  scale_shape_manual(values = c(19,21)) + 
  scale_size_manual(values = c(2,4)) + 
  scale_fill_manual(values = c('white','white')) + 
  facet_grid(sentType~model,scales = 'free_y',shrink = TRUE,space = 'free_y') + 
  theme(legend.position = 'none') + 
  xlab("Yellen relative to male Fed chairs") + 
  ylab('Tone Dimension')


pdf('../output/figures/tone_fed.pdf',width = 8,height = 7)
toplot %>%
  mutate(sentType = ifelse(out %in% paste0('SENT_',c('ATTACK_ON_AUTHOR','ATTACK_ON_COMMENTER','IDENTITY_ATTACK','INSULT','THREAT','combAttack')),'Aggression',
                           ifelse(out %in% paste0('SENT_',c('TOXICITY','SEVERE_TOXICITY','PROFANITY','SEXUALLY_EXPLICIT','FLIRTATION','OBSCENE','INFLAMMATORY','combToxic')),'Toxicity',
                                  ifelse(out %in% paste0('SENT_',c('INCOHERENT','UNSUBSTANTIAL','combIncoh')),'Incoherence',NA))),
         out = gsub('Attack On Commenter','Attack',
                    gsub('Combattack','INDEX: Aggression',
                         gsub('Combincoh','INDEX: Incoherence',
                              gsub('Combtoxic','INDEX: Toxicity',str_to_title(gsub('_',' ',gsub('SENT_','',out)))))))) %>%
  mutate(index = ifelse(grepl('INDEX',out),'index','dimension')) %>%
  mutate(model = ifelse(model == 'fedTone',"Tone of Utterances\nof Fed Chair",
                        "Tone of Utterances\nResponding to Fed Chair")) %>%
  filter(!is.na(sentType),
         !out %in% c('Attack On Author','Severe Toxicity'),
         !grepl('INDEX:',out)) %>%
  ggplot(aes(x = est,y = reorder(out,est),shape = index,fill = index,size = index)) + 
  geom_errorbarh(aes(xmin = est - 2*se,xmax = est + 2*se),height = .1,size = .5) + 
  geom_point() + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  theme_ridges() + 
  scale_shape_manual(values = c(19,21)) + 
  scale_size_manual(values = c(2,4)) + 
  scale_fill_manual(values = c('white','white')) + 
  facet_grid(sentType~model,scales = 'free_y',shrink = TRUE,space = 'free_y') + 
  theme(legend.position = 'none') + 
  xlab("Yellen relative to male Fed chairs") + 
  ylab('Tone Dimension')
dev.off()


# @(&!) APSR tables
require(stargazer)
for(d in names(mList)) {
  if(grepl('comb|SEVERE|AUTHOR|LIKELY',d)) {
    mList[[d]] <- NULL
  }
}
names(mList)

test <- stargazer(mList,
          keep = 'yellen|year|interrupted|SENT|nchars',
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'))

labs <- test[-which(grepl('^\\s+\\&|hline|LDA|FE|Observations|R\\$\\^\\{2|Note|tabular|table|Table|Date|caption|label|cline|multicolumn|ex',test))]
labs <- trimws(gsub(' \\&.*','',labs))
labs <- labs[-which(labs == '')]
labs <- gsub('lag\\)','lag',gsub('\\\\_',' ',gsub('scale\\(|SENT','',labs)))

stargazer(mList,
          keep = 'yellen|year|interrupted|SENT|nchars',
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),covariate.labels = labs,
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_12_table.tex')


for(d in names(mListRev)) {
  if(grepl('comb|SEVERE|AUTHOR|LIKELY',d)) {
    mListRev[[d]] <- NULL
  }
}
names(mListRev)

test <- stargazer(mListRev,
          keep = 'yellen|year|interrupted|SENT|nchars',
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'))

labs <- test[-which(grepl('^\\s+\\&|hline|LDA|FE|Observations|R\\$\\^\\{2|Note|tabular|table|Table|Date|caption|label|cline|multicolumn|ex',test))]
labs <- trimws(gsub(' \\&.*','',labs))
labs <- labs[-which(labs == '')]
labs <- gsub('lag\\)','lag',gsub('\\\\_',' ',gsub('scale\\(|SENT','',labs)))
stargazer(mListRev,
          keep = 'yellen|year|interrupted|SENT|nchars',covariate.labels = labs,
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_13_table_right.tex')

# DESCRIPTIVES
toAnalBern <- utterance_level %>% 
  filter(nchars > 60,date > as.Date('2006-01-01'),date < as.Date('2018-01-01'))
toAnalPow <- utterance_level %>% 
  filter(nchars > 60,date > as.Date('2014-01-01')) %>%
  mutate(yellenTime = !yellenTime)
toAnalFull <- utterance_level %>% 
  filter(nchars > 60)

labels <- NULL
mList <- list()
for(y in c('SENT_combAttack','SENT_combIncoh','SENT_combToxic')) {
  for(p in c('Bern','Pow','Full')) {
    summary(mList[[paste0(y,':',p)]] <- felm(as.formula(paste0('scale(',y,') ~ fedResp*yellenTime + ',
                                        paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                        ' + ',
                                        paste(paste0('scale(',dims[-which(grepl('comb',dims)|dims == y)],'_lag)'),collapse = ' + '),
                                        ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted + poly(year,3)',
                                        '| opensecretsID + chamber | 0 | opensecretsID')),
                      get(paste0('toAnal',p))))
    labels <- bind_rows(labels,
                        data.frame(t(summary(mList[[paste0(y,':',p)]])$coefficients['fedResp:yellenTimeTRUE',])) %>%
                          rename(est = Estimate,se = Cluster.s.e.,tstat = t.value,pval = Pr...t..) %>%
                          mutate(dimension = y,
                                 period = p))
  }
}

labels <- labels %>%
  mutate(star = ifelse(pval < .001,'***',
                       ifelse(pval < .01,'**',
                              ifelse(pval < .05,'*','')))) %>%
  mutate(lab = paste0(round(est,3),star,'\n(',round(se,3),')'))

pDescAgg <- toAnalFull %>%
  filter(nchars > 60,
         date > as.Date('2006-01-01')) %>%
  mutate(period = ifelse(date < as.Date('2014-01-01'),'preYellen',
                         ifelse(date < as.Date('2018-01-01'),'PostYellen','Yellen'))) %>%
  group_by(date,fedResp,period) %>%
  summarise(combAttack = mean(SENT_combAttack),
            combIncoh = mean(SENT_combIncoh),
            combToxic = mean(SENT_combToxic),.groups = 'drop') %>%
  mutate(grp = paste0(period,fedResp)) %>%
  ggplot(aes(x = date,y = combAttack,color = factor(fedResp),group = grp)) + 
  geom_vline(xintercept = as.Date('2014-01-01')) +
  geom_vline(xintercept = as.Date('2018-01-01')) + 
  geom_point() + 
  geom_smooth(method = 'lm') + 
  scale_color_manual(name = 'Responding To',values = c('grey50','grey10'),labels = c('Legislators','Fed Chair')) + 
  theme_ridges() + xlab('Date') + ylab('Pr(Attacking)') + 
  theme(legend.position = 'bottom') + 
  ylim(c(0,1)) + 
  ggtitle('DiD: Aggression Index') + 
  annotate(geom = 'label',x = c(as.Date('2014-01-01'),
                                as.Date('2018-01-01')),
           y = c(Inf,Inf),
           vjust = 1,
           size = 3,
           # hjust = c(1,0),
           fill = 'white',
           label = labels %>% 
             filter(dimension == 'SENT_combAttack',period %in% c('Bern','Pow')) %>% .$lab)

pDescInc <- toAnalFull %>%
  filter(nchars > 60,
         date > as.Date('2006-01-01')) %>%
  mutate(period = ifelse(date < as.Date('2014-01-01'),'preYellen',
                         ifelse(date < as.Date('2018-01-01'),'PostYellen','Yellen'))) %>%
  group_by(date,fedResp,period) %>%
  summarise(combAttack = mean(SENT_combAttack),
            combIncoh = mean(SENT_combIncoh),
            combToxic = mean(SENT_combToxic),.groups = 'drop') %>%
  mutate(grp = paste0(period,fedResp)) %>%
  ggplot(aes(x = date,y = combIncoh,color = factor(fedResp),group = grp)) + 
  geom_vline(xintercept = as.Date('2014-01-01')) +
  geom_vline(xintercept = as.Date('2018-01-01')) + 
  geom_point() + 
  geom_smooth(method = 'lm') + 
  scale_color_manual(name = 'Responding To',values = c('grey50','grey10'),labels = c('Legislators','Fed Chair')) + 
  theme_ridges() + xlab('Date') + ylab('Pr(Incoherent)') + 
  theme(legend.position = 'bottom') + 
  ylim(c(0,1)) + 
  ggtitle('DiD: Incoherence Index') + 
  annotate(geom = 'label',x = c(as.Date('2014-01-01'),
                                as.Date('2018-01-01')),
           y = c(Inf,Inf),
           vjust = 1,
           size = 3,
           # hjust = c(1,0),
           fill = 'white',
           label = labels %>% 
             filter(dimension == 'SENT_combIncoh',period %in% c('Bern','Pow')) %>% .$lab)

pDescTox <- toAnalFull %>%
  filter(nchars > 60,
         date > as.Date('2006-01-01')) %>%
  mutate(period = ifelse(date < as.Date('2014-01-01'),'preYellen',
                         ifelse(date < as.Date('2018-01-01'),'PostYellen','Yellen'))) %>%
  group_by(date,fedResp,period) %>%
  summarise(combAttack = mean(SENT_combAttack),
            combIncoh = mean(SENT_combIncoh),
            combToxic = mean(SENT_combToxic),.groups = 'drop') %>%
  mutate(grp = paste0(period,fedResp)) %>%
  ggplot(aes(x = date,y = combToxic,color = factor(fedResp),group = grp)) + 
  geom_vline(xintercept = as.Date('2014-01-01')) +
  geom_vline(xintercept = as.Date('2018-01-01')) + 
  geom_point() + 
  geom_smooth(method = 'lm') + 
  scale_color_manual(name = 'Responding To',values = c('grey50','grey10'),labels = c('Legislators','Fed Chair')) + 
  theme_ridges() + xlab('Date') + ylab('Pr(Toxic)') + 
  theme(legend.position = 'bottom') + 
  ylim(c(0,1)) + 
  ggtitle('DiD: Toxicity Index') + 
  annotate(geom = 'label',x = c(as.Date('2014-01-01'),
                                as.Date('2018-01-01')),
           y = c(Inf,Inf),
           vjust = 1,
           size = 3,
           # hjust = c(1,0),
           fill = 'white',
           label = labels %>% 
             filter(dimension == 'SENT_combToxic',period %in% c('Bern','Pow')) %>% .$lab)

# COMB
pComb <- toplot %>%
  filter(model == 'ols',
         type == 'legTone') %>%
  mutate(sentType = ifelse(out %in% paste0('SENT_',c('ATTACK_ON_AUTHOR','ATTACK_ON_COMMENTER','IDENTITY_ATTACK','INSULT','THREAT','combAttack')),'Aggression',
                           ifelse(out %in% paste0('SENT_',c('TOXICITY','SEVERE_TOXICITY','PROFANITY','SEXUALLY_EXPLICIT','FLIRTATION','OBSCENE','INFLAMMATORY','combToxic')),'Toxicity',
                                  ifelse(out %in% paste0('SENT_',c('INCOHERENT','UNSUBSTANTIAL','combIncoh')),'Incoherence',NA))),
         out = gsub('Attack On Commenter','Attack',
                    gsub('Combattack','INDEX: Aggression',
                         gsub('Combincoh','INDEX: Incoherence',
                              gsub('Combtoxic','INDEX: Toxicity',str_to_title(gsub('_',' ',gsub('SENT_','',out)))))))) %>%
  mutate(index = ifelse(grepl('INDEX',out),'index','dimension')) %>%
  filter(!is.na(sentType),
         !out %in% c('Attack On Author','Severe Toxicity'),
         !grepl('INDEX:',out)) %>%
  ggplot(aes(x = est,y = reorder(out,est),shape = index,fill = index,size = index)) + 
  geom_errorbarh(aes(xmin = est - 2*se,xmax = est + 2*se),height = .1,size = .5) + 
  geom_point() + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  theme_ridges() + 
  scale_shape_manual(values = c(19,21)) + 
  scale_size_manual(values = c(2,4)) + 
  scale_fill_manual(values = c('white','white')) + 
  facet_grid(sentType~.,scales = 'free',shrink = TRUE,space = 'free') + 
  theme(legend.position = 'none') + 
  xlab("Yellen coefficient") + 
  ylab('') + 
  labs(title = 'Interlocutor FE',subtitle = 'Yellen Relative to Male Chairs')
  


require(cowplot)
leg <- get_legend(pDescAgg + theme(legend.text = element_text(size = 8),legend.title = element_text(size = 9)))
pdf('../Paper/Figures/toxic_comb.pdf',width = 7,height = 7)
ggdraw() + 
  draw_plot(pDescAgg + theme(legend.position = 'none',
                             plot.margin = unit(c(0,0,-.5,0), "cm")) + xlab(''),x = 0,y = .68,width = .4,height = .32) + 
  draw_plot(pDescInc + theme(legend.position = 'none',
                             plot.margin = unit(c(0,0,-.5,0), "cm")) + xlab(''),x = 0,y = .36,width = .4,height = .32) + 
  draw_plot(pDescTox + theme(legend.position = 'none',
                             plot.margin = unit(c(0,0,-.5,0), "cm")) + xlab(''),x = 0,y = .04,width = .4,height = .32) + 
  draw_plot(leg,x = .05,y = 0,width = .4,height = .05) + 
  draw_plot(pComb + theme(legend.position = 'none',
                          plot.margin = unit(c(0,0,0,0), "cm")),x = .4,y = 0,width = .6,height = 1)
dev.off()

names(mList)
test <- stargazer(mList,
                  keep = 'yellen|fedResp|year|interrupted|SENT|nchars',
                  add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                                   c('Chamber FE',rep('Y',12)),
                                   c('Speaker FE',rep('Y',12))),
                  column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_|:.*','',names(mList)))),
                  star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
                  keep.stat = c('n','rsq'))

labs <- test[-which(grepl('^\\s+\\&|hline|LDA|FE|Observations|R\\$\\^\\{2|Note|tabular|table|Table|Date|caption|label|cline|multicolumn|ex',test))]
labs <- trimws(gsub(' \\&.*','',labs))
labs <- labs[-which(labs == '')]
labs <- trimws(gsub('lag\\)','lag',gsub('\\\\_',' ',gsub('scale\\(|SENT','',labs))))
stargazer(mList,
          keep = 'yellen|fedResp|year|interrupted|SENT|nchars',
          covariate.labels = labs,
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_|:.*','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_13_table_left.tex')



# Daughters
toplot <- NULL
mList <- list()
for(dim in colnames(utterance_level %>% select(matches('SENT_',ignore.case = F)))) {
  if(grepl('error|_lag|DEM|GOP',dim)) { next }
  summary(mList[[dim]] <- felm(as.formula(paste0(dim,' ~ respondingTo*anyDaughters + ',
                                        paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                        ' + ',
                                        paste(paste0('scale(',dims[-which(grepl('comb',dims)|dims == y)],'_lag)'),collapse = ' + '),
                                        ' + poly(scale(log(nchars_lag+1)),3) + poly(scale(log(tot_utterances)),3) + interrupted + poly(year,3)',
                                        '| opensecretsID + chamber | 0 | opensecretsID + docID')),
                      utterance_level %>%
                        filter(grepl('FED',respondingTo),
                               nchars > 60) %>%
                        mutate(#respondingTo = ifelse(respondingTo == 'ChairpersonYellen','Yellen','Other FED Chairs'),
                          anyDaughters = ifelse(nDaughters > 0,1,0),
                          propDaughters = nDaughters / nKids,
                          Male = ifelse(gender == 'M',1,0))))
  
  toplot <- bind_rows(toplot,as_tibble(do.call(rbind,interaction_plot_continuous(mList[[dim]],incr = 1)) %>%
                                         mutate(chair = gsub('.*Chairperson|:.*','',rownames(.)),
                                                est = gsub('Interaction: ','',gsub(' \\(','\n(',est)),
                                                dimension = dim)  ))
  
}


toplot2 <- toplot %>%
  mutate(sig = ifelse(pval < .05,'sig','insig')) %>%
  filter(grepl('comb',dimension)) %>%
  mutate(chair = factor(ifelse(grepl('YELLEN',chair),'Yellen',
                        ifelse(grepl('GREENSPAN',chair),'Greenspan',
                               ifelse(grepl('POWELL',chair),'Powell',NA))),
                        levels = c('Greenspan','Powell','Yellen')),
         est = ifelse(pval < .01 & pval > .001,gsub('\\*{3}','**',est),
                      ifelse(pval < .05 & pval > .01,gsub('\\*\\*','*',est),
                             ifelse(pval < .1 & pval > .05,gsub('\\*','',est),est))),
         dimension = factor(gsub('SENT_combAttack','Aggression',
                                 gsub('SENT_combToxic','Toxic',
                                      gsub('SENT_combIncoh','Incoherent',dimension))),
                            levels = c('Aggression','Incoherent','Toxic')))


pdf('../Paper/Figures/toxicity_interrupt_mfx_daughters.pdf',width = 7,height = 5)
toplot2 %>%
  # filter(grepl('Yellen',chair)) %>%
  ggplot(aes(x = x_2,y = delta_1,label = est)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_point() + 
  geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) + 
  scale_x_continuous(breaks = c(0,1),labels = c('None','Any'),expand = c(.2,.2)) + 
  # facet_grid(dimension~.) + 
  facet_grid(dimension~chair) + 
  # scale_alpha_manual(name = 'Significance',values = c(.4,1),labels = c('Insig','Sig @95%')) + 
  geom_text(data = toplot2 %>%
              # filter(grepl('Yellen',chair)) %>%
              group_by(dimension,chair,sig) %>%
              slice(1) %>%
              mutate(x_2 = .5,delta_1 = .2,
                     est = gsub('Interaction: ','',gsub(' \\(','\n(',est))),vjust = 1,hjust = .5,size = 3,show.legend = F) + 
  theme_ridges() + 
  # coord_flip() +
  xlab('Daughters') + 
  ylab('Effect of Chair\ncompared to Bernanke') + 
  # scale_x_discrete(expand = c(.1,.1)) + 
  theme(strip.background = element_rect(fill = 'white',color = 'black',linetype = 'solid'),
        strip.text = element_text(size = 9),legend.position = 'bottom')
dev.off()


for(d in names(mList)) {
  if(!grepl('comb',d)) {
    mList[[d]] <- NULL
  }
}
names(mList)
test <- stargazer(mList,
                  keep = 'respondingTo|Daughters|year|interrupted|SENT|nchars',
                  add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                                   c('Chamber FE',rep('Y',12)),
                                   c('Speaker FE',rep('Y',12))),
                  column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_|:.*','',names(mList)))),
                  star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
                  keep.stat = c('n','rsq'))

labs <- test[-which(grepl('^\\s+\\&|hline|LDA| FE|Observations|R\\$\\^\\{2|Note|tabular|table|Table|Date|caption|label|cline|multicolumn|ex',test))]
labs <- trimws(gsub(' \\&.*','',labs))
labs <- labs[-which(labs == '')]
labs <- trimws(gsub('lag\\)','lag',gsub('\\\\_',' ',gsub('scale\\(|SENT|respondingToFED','',labs))))
stargazer(mList,
          keep = 'respondingTo|Daughters|year|interrupted|SENT|nchars',
          covariate.labels = labs,
          add.lines = list(c('100 LDA Topic Loadings',rep('Y',12)),
                           c('Chamber FE',rep('Y',12)),
                           c('Speaker FE',rep('Y',12))),
          column.labels = gsub('ITY|ENTITY|UALLY|ICIT|ATION|ENTER|MATORY|STANTIAL|TAC| ON|ERENT','',gsub('_',' ',gsub('SENT_|:.*','',names(mList)))),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = '../Paper/Tables/fig_14_table.tex')



pdf('../Paper/Figures/toxicity_interrupt_mfx_daughters_SI.pdf',width = 7,height = 5)
toplot2 %>%
  # filter(dimension == 'Attacking') %>%
  ggplot(aes(x = x_2,y = delta_1,label = est,alpha = sig)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_point() + 
  geom_errorbar(aes(ymin = lb,ymax = ub),width = .1) + 
  scale_x_continuous(breaks = c(0,1),labels = c('None','Any'),expand = c(.2,.2)) + 
  # facet_grid(dimension~.) + 
  facet_grid(chair~dimension) + 
  scale_alpha_manual(name = 'Significance',values = c(.4,1),labels = c('Insig','Sig @95%')) + 
  geom_text(data = toplot2 %>%
              # filter(dimension == 'Attacking') %>%
              group_by(dimension,chair,sig) %>%
              slice(1) %>%
              mutate(x_2 = .5,delta_1 = Inf,
                     est = gsub('Interaction: ','',gsub(' \\(','\n(',est))),vjust = 1,hjust = .5,size = 3,show.legend = F) + 
  theme_ridges() + 
  # coord_flip() +
  xlab('Daughters') + 
  ylab('Effect of Fed Chair\ncompared to Bernanke') + 
  # scale_x_discrete(expand = c(.1,.1)) + 
  theme(strip.background = element_rect(fill = 'white',color = 'black',linetype = 'solid'),
        strip.text = element_text(size = 9),legend.position = 'bottom')
dev.off()













# OLD
# For SI
# toplot2 <- toplot %>%
#   mutate(samp = ifelse(grepl('_FULL',dimension),'All Interactions','Interruptions Only'),
#          dimension = str_to_title(gsub('_',' ',gsub('_ON_',': ',gsub('_(INT|FULL)','',dimension))))) %>%
#   left_join(toplot %>%
#               filter(chair == 'Yellen') %>%
#               mutate(samp = ifelse(grepl('_FULL',dimension),'All Interactions','Interruptions Only'),
#                      dimension = str_to_title(gsub('_',' ',gsub('_ON_',': ',gsub('_(INT|FULL)','',dimension))))) %>%
#               select(chair,dimension,pval,samp) %>%
#               distinct() %>%
#               filter(!grepl('Comb',dimension)) %>%
#               group_by(samp) %>%
#               arrange(pval) %>% #data.frame() %>%
#               mutate(rank = row_number()) %>%
#               mutate(refPval = (rank / (nrow(.)/2))*.05) %>%
#               mutate(FWERsig = ifelse(pval < refPval,1,0)) %>%
#               data.frame()) %>%
#   filter(complete.cases(.)) %>%
#   # mutate(sig = ifelse(grepl('\\*{2,}',est),'Sig','Insig')) %>%
#   mutate(xlab = factor(ifelse(x_2 == 0,'None','Any'),levels = c('None','Any')),
#          chair = factor(chair,levels = c('Greenspan','Yellen','Powell')),
#          intCoef = as.numeric(gsub('(\\*)*\\\n.*','',est)))
# 
# 
# toplot2 <- toplot2 %>%
#   mutate(dimension = factor(dimension,levels = c(toplot2 %>% filter(chair == 'Yellen',samp == 'All Interactions') %>% select(dimension,intCoef) %>% distinct() %>% arrange(intCoef) %>% .$dimension)),
#          # est2 = ifelse(sig == 'Sig',gsub('\\*+','*',est),gsub('\\*+','',est)),
#          est = gsub('\\*\\\n','\n',est)) %>%
#   mutate(sig = factor(ifelse(FWERsig == 1,'FWER',
#                              ifelse(grepl('\\*',est),'95%','Insig')),levels = c('Insig','95%','FWER')))
# 
# 
# toplot2 <- toplot2 %>%
#   mutate(grp = ifelse(grepl('Attack|Insult|Threat',dimension),'Attacking',
#                       ifelse(grepl('Incoherent|Inflammatory|Likely|Unsubstantial',dimension),'Incoherent','Toxic')),
#          dim2 = paste0(grp,': \n',gsub('Attack: ','',dimension)))
# 
# pdf('../Paper/Figures/SI_toxicity_full.pdf',width = 9,height = 11)
# toplot2 %>%
#   filter(chair == 'Yellen') %>%
#   ggplot(aes(x = xlab,y = delta_1,label = est,alpha = sig,size = sig,shape = sig)) + 
#   geom_hline(yintercept = 0) + 
#   geom_errorbar(aes(ymin = lb,ymax = ub),width = 0,size = .7) + 
#   geom_point(fill = 'white') + 
#   # geom_line(linetype = 'dashed',aes(alpha = sig)) + 
#   # geom_line(aes(x = x_2,y = lb,alpha = sig)) + 
#   # geom_line(aes(x = x_2,y = ub,alpha = sig)) + 
#   geom_text(data = toplot2 %>%
#               filter(chair == 'Yellen')  %>%
#               group_by(chair,dim2,est,samp,sig) %>%
#               summarise(xlab = diff(range(as.numeric(xlab)))/2+1) %>%
#               ungroup() %>%
#               mutate(delta_1 = Inf),
#             vjust = 1,alpha = 1,size = 3) +
#   facet_grid(dim2~samp,shrink = T,space = 'free') + 
#   scale_alpha_manual(name = 'Significance',values = c(.3,1,1)) + 
#   scale_size_manual(name = 'Significance',values = c(1,1,2)) + 
#   scale_shape_manual(name = 'Significance',values = c(19,19,21)) + 
#   theme_ridges() + 
#   xlab('Daughters') + ylab('Effect of Yellen\ncompared to Bernanke') + 
#   theme(strip.background = element_rect(size = .5,color = 'black',fill = 'white',linetype = 'solid'),
#         strip.text.y = element_text(angle = 0,size = 9),
#         legend.position = 'bottom')
# dev.off()
# 
# 
# toplot <- utterance_level %>%
#   filter(grepl('FED',respondingTo),
#          !grepl('FED|EXPERT',opensecretsID),
#          nchars > 100) %>%
#   mutate(speaker = ifelse(grepl('YELLEN',respondingTo),'Yellen','Other FED Chairs'),
#          y1 = SENT_ATTACK_ON_AUTHOR)
# 
# (p_INT_2 <-  toplot %>%
#     filter(interruptor == 1) %>%
#     ggplot(aes(x = y1,fill = speaker)) + 
#     # geom_density_ridges(size = 0,alpha = .5,stat = 'binline') +
#     geom_density(size = 0,alpha = .5) + 
#     scale_fill_manual(guide = 'none',values = c('cyan','red')) + 
#     xlim(c(0,1)) + 
#     theme_ridges() + xlab('') + ylab(''))
# 
# 
# (tRes <- t.test(p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Other FED Chairs')],
#                 p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Yellen')]))
# 
# wRes <- wilcox.test(p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Other FED Chairs')],
#                     p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Yellen')])
# medNon <- median(p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Other FED Chairs')],na.rm=T)
# medPers <- median(p_INT_2$data$y1[which(p_INT_2$data$speaker == 'Yellen')],na.rm=T)
# wRes$p.value
# 
# (p_INT_1 <- toplot %>%
#     filter(interruptor == 1) %>%
#     ggplot(aes(y = y1,x = speaker,fill = speaker)) + 
#     geom_boxplot() + 
#     scale_fill_manual(guide = 'none',values = c('cyan','red')) + 
#     theme_ridges() + 
#     ylab('Pr(Interruption is an Attack)') + xlab('') + 
#     coord_flip() + 
#     ylim(c(0,1)) + 
#     annotate(geom = 'text',x = .8,y = medNon,label = paste0('Median: ',round(medNon,2)),vjust = 1,hjust = 0,size = 3) + 
#     annotate(geom = 'text',x = 2.2,y = medPers,label = paste0('Median: ',round(medPers,2)),vjust = 0,hjust = 0,size = 3) + 
#     annotate(geom = 'text',x = 1.5,y = .5,label = paste0('p = ',round(wRes$p.value,2)),vjust = 1,hjust = 0,size = 3))
# 
# 
# 
# (p_FULL_2 <-  toplot %>%
#     ggplot(aes(x = y1,fill = speaker)) + 
#     # geom_density_ridges(size = 0,alpha = .5,stat = 'binline') +
#     geom_density(size = 0,alpha = .5) + 
#     scale_fill_manual(guide = 'none',values = c('cyan','red')) + 
#     xlim(c(0,1)) + 
#     theme_ridges() + xlab('') + ylab(''))
# 
# 
# (tRes <- t.test(p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Other FED Chairs')],
#                 p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Yellen')]))
# 
# wRes <- wilcox.test(p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Other FED Chairs')],
#                     p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Yellen')])
# medNon <- median(p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Other FED Chairs')],na.rm=T)
# medPers <- median(p_FULL_2$data$y2[which(p_FULL_2$data$speaker == 'Yellen')],na.rm=T)
# wRes$p.value
# 
# (p_FULL_1 <- toplot %>%
#     ggplot(aes(y = y2,x = speaker,fill = speaker)) + 
#     geom_boxplot() + 
#     scale_fill_manual(guide = 'none',values = c('cyan','red')) + 
#     theme_ridges() + 
#     ylab('Pr(Interaction is an Attack)') + xlab('') + 
#     coord_flip() + 
#     ylim(c(0,1)) + 
#     annotate(geom = 'text',x = .8,y = medNon,label = paste0('Median: ',round(medNon,2)),vjust = 1,hjust = 0,size = 3) + 
#     annotate(geom = 'text',x = 2.2,y = medPers,label = paste0('Median: ',round(medPers,2)),vjust = 0,hjust = 0,size = 3) + 
#     annotate(geom = 'text',x = 1.5,y = .5,label = paste0('p = ',round(wRes$p.value,2)),vjust = 1,hjust = 0,size = 3))
# 
# 
# pdf('../Paper/Figures/interruptions_attack.pdf',width = 9,height = 6)
# cowplot::plot_grid(p_INT_2, p_FULL_2, p_INT_1 + theme(axis.text.y = element_text(size = 7)), p_FULL_1 + theme(axis.text.y = element_text(size = 7)),
#                    ncol = 2, rel_heights = c(3, 1),
#                    align = 'v', axis = 'lr')  
# dev.off()
# 
# 
